def str_to_hex_array(s):
    """将字符串转换为10进制整数数组（小端序：从右向左每2字符一组，低位在前），同时返回16进制字符串列表"""
    if s.startswith('0x'):
        s = s[2:]
    if len(s) % 2 != 0:
        raise ValueError("输入字符串长度必须为偶数（不含0x前缀）")
    
    dec_array = []
    hex_str_list = []
    for i in range(len(s)-2, -1, -2):
        hex_str = s[i:i+2]
        dec_val = int(hex_str, 16)
        dec_array.append(dec_val)
        hex_str_list.append(f"0x{hex_str}")
    return dec_array, hex_str_list

def split_into_groups(arr, group_size=8):
    """将数组拆分为指定大小的子数组（每组8个元素）"""
    groups = []
    for i in range(0, len(arr), group_size):
        group = arr[i:i+group_size]
        if len(group) < group_size:
            group += [0] * (group_size - len(group))
        groups.append(group)
    return groups

def transpose_groups(groups):
    """对vs2分组进行转置（按列取元素）"""
    while len(groups) < 8:
        groups.append([0]*8)
    transposed = []
    for i in range(8):
        new_group = [groups[j][i] for j in range(8)]
        transposed.append(new_group)
    return transposed

def matrix_dot_product(mat1, mat2):
    """矩阵点乘：vs1的第i行 与 vs2转置后的第j行 点乘，结果为result[i][j]"""
    result = []
    print("矩阵点乘计算过程（行与行对应点乘）：")
    for i in range(8):  # 遍历vs1的行（结果行索引）
        row_result = []
        for j in range(8):  # 遍历vs2转置后的行（结果列索引）
            # 获取vs1的第i行 和 vs2转置后的第j行
            row1 = mat1[i]  # vs1矩阵第i行
            row2 = mat2[j]  # vs2转置后矩阵第j行（关键修正：使用j索引）
            
            # 计算对应位置乘积并求和
            products = [row1[k] * row2[k] for k in range(8)]
            sum_val = sum(products)
            
            # 打印计算过程
            print(f"  结果[{i}][{j}] = ", end="")
            print(" + ".join([f"{a}×{b}" for a, b in zip(row1, row2)]), end="")
            print(f" = {sum_val}")
            
            row_result.append(sum_val)
        result.append(row_result)
        print()  # 行之间空行
    return result

def print_full_hex_array(hex_list, name):
    """完整打印16进制格式数组"""
    print(f"{name}（共{len(hex_list)}个元素，16进制表示）：")
    for i in range(0, len(hex_list), 16):
        print(f"  {hex_list[i:i+16]}")
    print()

def print_full_dec_array(arr, name):
    """完整打印10进制格式数组"""
    print(f"{name}（共{len(arr)}个元素，10进制表示）：")
    for i in range(0, len(arr), 16):
        print(f"  {arr[i:i+16]}")
    print()

def print_full_groups(groups, name):
    """完整打印分组数组"""
    print(f"{name}（共{len(groups)}组，每组8个元素，10进制表示）：")
    for i, group in enumerate(groups):
        print(f"  第{i+1}组：{group}")
    print()

def main():
    # 输入字符串
    str1 = "0x00000000163e57550067009d5e040ba200000000497fdf1a00a3007e8095ad5300000000cb6afb5b000300207a0fbf83000000008686e85900fe004a33f1f3d4"
    str2 = "0x00000000b83cf7e00000000025ca2825000000007a6e7cc2000000001ab3661c000000005748d972000000000bc6dead00000000041300d7000000001f9d941e"
    
    # 处理vs1
    dec_array1, hex_array1 = str_to_hex_array(str1)
    print_full_hex_array(hex_array1, "vs1转换后的原始数组")
    print_full_dec_array(dec_array1, "vs1转换后的数值数组")
    
    vs1_groups = split_into_groups(dec_array1)
    print_full_groups(vs1_groups, "vs1拆分后的分组（作为矩阵行）")
    
    # 处理vs2
    dec_array2, hex_array2 = str_to_hex_array(str2)
    print_full_hex_array(hex_array2, "vs2转换后的原始数组")
    print_full_dec_array(dec_array2, "vs2转换后的数值数组")
    
    vs2_groups = split_into_groups(dec_array2)
    print_full_groups(vs2_groups, "vs2原始拆分后的分组")
    
    vs2_transposed = transpose_groups(vs2_groups)
    print_full_groups(vs2_transposed, "vs2转置后的分组（作为矩阵行）")
    
    # 构建8x8矩阵（vs1和转置后的vs2）
    vs1_matrix = vs1_groups[:8] if len(vs1_groups)>=8 else vs1_groups + [[0]*8]*(8-len(vs1_groups))
    vs2_matrix = vs2_transposed[:8]
    
    # 打印矩阵
    print("用于点乘的8x8矩阵（行与行对应计算）：")
    print("vs1矩阵（行）：")
    for i, row in enumerate(vs1_matrix):
        print(f"  第{i+1}行：{row}")
    print()
    print("vs2转置后矩阵（行）：")
    for j, row in enumerate(vs2_matrix):
        print(f"  第{j+1}行：{row}")
    print()
    
    # 行与行点乘计算（修正索引逻辑）
    result = matrix_dot_product(vs1_matrix, vs2_matrix)
    
    # 输出最终结果
    print("8x8矩阵点乘结果（10进制）：")
    for i, row in enumerate(result):
        print(f"  第{i+1}行：{row}")

if __name__ == "__main__":
    main()
